package com.example.sd;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;

public class MainActivity3 extends AppCompatActivity {

    public static final String[] PERMISSION_SDCARD = new String[]{
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    public static final int REQUEST_CODE_SDCARD = 1;

    public static final String TAG = "MainActivity3";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        // 饿汉式申请权限
        PermissionUtils.checkPermission(this,PERMISSION_SDCARD,REQUEST_CODE_SDCARD);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_SDCARD) {
            if(PermissionUtils.checkGrantResults(grantResults)){
                ToastUtils.show(this,"获取读写权限成功");
            }else{
                ToastUtils.show(this,"获取读写权限失败");
                jump2PermissionSet();
            }
        }
    }

    /**
     * 跳转到App info界面
     */
    private void jump2PermissionSet() {
        String packageName = getPackageName();
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("package:" + packageName));
        startActivity(intent);
    }
}